import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:zk_flutter_bluetooth_print/zk_flutter_bluetooth_print.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _zkFlutterBluetoothPrintPlugin = ZkFlutterBluetoothPrint();

  @override
  void initState() {
    super.initState();
  }

  /// 打印图片
  printImg() async {
    try {
      Map params = {
        "BDAddress": "00:32:69:05:87:97",
        // "width": "574",
        // "height": "982",
        "width": "555",
        "height": "785",
        // "BDAddress": "00:11:58:05:78:39",
        "url":
            "https://udfs.unisiot.com/group1/M00/0D/09/rBUBAmXpbPuAbiKxAAI17Kgf39g345.png",
        // "url": "https://www.baidu.com/img/flexible/logo/pc/result@2.png",
      };
      var result = await _zkFlutterBluetoothPrintPlugin.printImage(params);
      print(result);
    } on PlatformException {
      print('Failed to printImage.');
    }
  }

  /// 打印base64图片
  printBase64Img() async {
    try {
      Map params = {
        "BDAddress": "00:32:69:05:87:97",
        // "BDAddress": "00:11:58:05:78:39",
        "width": "555",
        "height": "785",
        "base64":
            "",
        // "url": "https://www.baidu.com/img/flexible/logo/pc/result@2.png",
      };
      await _zkFlutterBluetoothPrintPlugin.printBase64Image(params);
    } on PlatformException {
      print('Failed to printBase64Image.');
    }
  }

  connect() async {
    try {
      Map params = {
        "BDAddress": "60:6E:41:35:59:8D",
      };
      var result = await _zkFlutterBluetoothPrintPlugin.connect(params);
      print('result----$result');
    } on PlatformException {
      print('Failed to connect.');
    }
  }

  disConnect() async {
    try {
      var result = await _zkFlutterBluetoothPrintPlugin.disConnect();
      print('result----$result');
    } on PlatformException {
      print('Failed to disConnect.');
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              Text('Running on: $_platformVersion\n'),
              TextButton(
                onPressed: () {
                  connect();
                },
                child: const Text('连接打印机'),
              ),
              TextButton(
                onPressed: () {
                  printImg();
                },
                child: const Text('打印'),
              ),
              TextButton(
                onPressed: () {
                  printBase64Img();
                },
                child: const Text('打印base64'),
              ),
              TextButton(
                onPressed: () {
                  disConnect();
                },
                child: const Text('断开连接'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
